/*****************************
 *@file: LinkBulletComp
 *@author: 陈吕唐
 *@desc: 链接子弹
 *@date: 2024-03-13	13:37
 *****************************/
import { Collider2D, IPhysics2DContact, _decorator } from 'cc';
import GameSingle from '../GameSingle';
import GameUtils from '../GameUtils';
import { LifeEnum } from '../core/enum/LifeEnum';
import { PhysicsGroupEnum } from '../enum/PhysicsGroupEnum';
import { BaseBulletComp } from './BaseBulletComp';
const { ccclass, property } = _decorator;

@ccclass('LinkBulletComp')
export class LinkBulletComp extends BaseBulletComp {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    private number: number = 3;
    /***************************************************************
     *PropertyUI 引用的控件
     ***************************************************************/
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/
    protected override onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact): void {
        super.onBeginContact(selfCollider, otherCollider, contact);
        switch (otherCollider.group) {
            case PhysicsGroupEnum.BORDER:
                this.recycleBullet();
                break;
            default:
                break;
        }

        let fight = GameUtils.getFightByCollider(otherCollider);
        if (!fight) {
            return;
        }

        let monster = GameSingle.single.scene.monsters;
        let array = monster.filter((item) => item.lifeState == LifeEnum.LIFE);

        if (array.length > this.number) {
            array = array.sort((a, b) => {
                let lenA = Math.abs(a.node.position.x - fight.node.position.x) + Math.abs(a.node.position.y - fight.node.position.y);
                let lenB = Math.abs(b.node.position.x - fight.node.position.x) + Math.abs(b.node.position.y - fight.node.position.y);
                return lenA - lenB;
            });

            array = array.splice(0, this.number);
        }

        for (let i = 0; i < array.length; i++) {
            const element = array[i];
            element.hurt(this);
        }

        this.recycleBullet();
    }

    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/

}


